﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using FHost1.Utility;

namespace FHost1
{
    public partial class FormSearch : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                SearchFiles();
            }
        }

        private void SearchFiles()
        {
            int pageNo = 1;
            int type = 0;
            int? totalRec = 0;
            string searchTxt = "";
            if (Request.QueryString.AllKeys.Contains("page"))
            {
                pageNo = Convert.ToInt32(Request.QueryString["page"]);
            }

            if (Request.QueryString.AllKeys.Contains("search"))
            {
                searchTxt = Request.QueryString["search"];
            }

            if (Request.QueryString.AllKeys.Contains("type"))
            {
                type = Convert.ToInt32(Request.QueryString["type"]);
            }

            if (!string.IsNullOrWhiteSpace(searchTxt))
            {
                txtSearch.Text = searchTxt;
                using (FHostDataContext dataContext = new FHostDataContext())
                {
                    var result = dataContext.SearchFiles(searchTxt, pageNo, type, ref totalRec);
                    GridSearchResult.DataSource = result;
                    GridSearchResult.DataBind();
                }
            }
        }

        protected void btnSearch_Click(object sender, EventArgs e)
        {
            Response.Redirect(string.Format("Search.aspx?search={0}&page={1}&type={2}", txtSearch.Text, 1, ddlSearch.SelectedValue));
        }
    }
}